package cn.kgc.crowd1048.fundingadmin.service.impl;

import cn.kgc.crowd1048.fundingadmin.entity.TAdmin;
import cn.kgc.crowd1048.fundingadmin.entity.TRole;
import cn.kgc.crowd1048.fundingadmin.mapper.TRoleMapper;
import cn.kgc.crowd1048.fundingadmin.service.TRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.List;

@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper,TRole> implements TRoleService {
    @Autowired
    TRoleMapper tRoleMapper;

    @Override
    public PageInfo<TRole> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<TRole> pageInfo = null;
        QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
        if (keyword!=null && keyword.length()>0){
            queryWrapper.like("login_acct",keyword).or().like("user_name",keyword).or().like("email",keyword);


        }
        List<TRole> list = list(queryWrapper);
        pageInfo=new PageInfo<>(list);
        return pageInfo;

    }

//    返回的值
    @Override
    public List<TRole> getRoleListByIdList(List<Integer> roleIdList) {
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<TRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TRole::getId, roleIdList);

        // 执行查询
        return tRoleMapper.selectList(queryWrapper);

    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        // 使用 LambdaQueryWrapper 构建删除条件
        LambdaQueryWrapper<TRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TRole::getId, roleIdList);

        // 执行删除
        tRoleMapper.delete(queryWrapper);
    }

//    添加的方法
    @Override
    public void saveRole(String roleName) {
       TRole tRole = new TRole();
       tRole.setId(0);
       tRole.setName(roleName);
        save(tRole);
    }

    @Override
    public void updateRole(TRole role) {
        updateRole(role);
    }
}
